/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.fa.supporters;

import java.awt.Color;
import java.awt.Component;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.border.EtchedBorder;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author tatung
 */
public class TransactionCellColorRenderer extends JLabel implements TableCellRenderer {
    private String columnName;    
    private Color curColor;
    private final Color separatingRowColor = Color.WHITE;
    private final Color dataRowColor = new Color(0xe8, 0xf2, 0xfe);
    private final Color summaryRowColor = new Color(0x79, 0xf3, 0xf3);
    
    
    public TransactionCellColorRenderer(String column)
    {
        this.columnName = column;
        curColor = separatingRowColor;
        setOpaque(true);
    }
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column)
    {

        Object columnValue=table.getValueAt(row,table.getColumnModel().getColumnIndex(columnName));
        Object prevRowColumnValue = null;
        if(row > 0){
            // if this is not the first row
            prevRowColumnValue = table.getValueAt(row-1, table.getColumnModel().getColumnIndex(columnName));
        }
        if (value != null){
            setText(value.toString());
        }else{
            setText("");
        }

        setBackground(dataRowColor);
        setForeground(table.getForeground());

        if(isSelected){
            setBackground(table.getSelectionBackground());
            setForeground(table.getSelectionForeground());
        }else{                        
            // set color for summary row and separating row
            if(table.getValueAt(row, table.getColumnModel().getColumnIndex("Hàng"))!=null &&
                    table.getValueAt(row, table.getColumnModel().getColumnIndex("Hàng")).equals("Tổng S.lg")){
                //summary row                
                setBackground(summaryRowColor);
                
                
            }else if(table.getValueAt(row, table.getColumnModel().getColumnIndex("Hàng"))==null ||
                        (table.getValueAt(row, table.getColumnModel().getColumnIndex("Hàng"))!=null &&
                        table.getValueAt(row, table.getColumnModel().getColumnIndex("Hàng")).equals(""))){
                //separating row
                setBackground(separatingRowColor);
            }
            


            //JOptionPane.showMessageDialog(null, columnValue.toString());
//            if(prevRowColumnValue == null){
//                //first row
//                //setBackground(firstRowColor);
//            }else if(prevRowColumnValue != null){
//                if(!prevRowColumnValue.equals(columnValue) && column == 0){
//                    // if this cell is different from previous row's corresponding cell
//                    if(curColor == separatingRowColor){
//                        curColor = dataRowColor;
//                    }else if(curColor == dataRowColor){
//                        curColor = separatingRowColor;
//                    }
//                }
//                //setBackground(curColor);
//            }
            
        }
        return this;
    }
    
}
